package calvin.study.rabbitmq.deleyqueue;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import calvin.study.rabbitmq.config.RabbitMqConnection;

public class DelayQueue {
	
	public static void main(String[] args) {
		Connection connection = null;
		Channel channel = null;

		RabbitMqConnection mqConnection = RabbitMqConnection.getInstance();

		List<Connection> connections = mqConnection.getMqConnections();

		connection = connections.get(0);
		try {
			channel = connection.createChannel();
			
			//订单过期交换机
			channel.exchangeDeclare("ORDER_DELAY_DIRECT_EXCHANGE", BuiltinExchangeType.DIRECT.getType());
			
			//死信交换机
			channel.exchangeDeclare("ORDER_DELAY_DLX_TOPIC_EXCHANGE", BuiltinExchangeType.TOPIC.getType());
			 
			Map<String,Object> orderDelayQueueParams = new HashMap<String,Object>();
			orderDelayQueueParams.put("x-message-ttl", 30000);
			orderDelayQueueParams.put("x-dead-letter-exchange", "ORDER_DELAY_DLX_TOPIC_EXCHANGE");//过期订单分派到死信交换机
			
			//生命订单过期队列
			channel.queueDeclare("order_delay_queue", false, false, false, orderDelayQueueParams);
			
			channel.queueBind("order_delay_queue", "ORDER_DELAY_DIRECT_EXCHANGE", "order.orderinfo.666");
			 
			//
			channel.queueDeclare("order_delay_exe_queue", false, false, false, null);
			
			
			channel.queueBind("order_delay_exe_queue", "ORDER_DELAY_DLX_TOPIC_EXCHANGE", "#");
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				if(null != channel) {
					channel.close();
				}
				if(null!=connection) {
					connection.close();
				}
			} catch (IOException e) { 
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (TimeoutException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
